// src/api/pluginData.ts
import http from './http'

// 定义数据类型
export interface PluginDataStatistics {
  totalBatches: number
  totalRecords: number
  todayBatches: number
  todayRecords: number
  batchByStatus: Array<{
    status: string
    count: number
  }>
  dataByCategory: Array<{
    category: string
    count: number
  }>
}

export interface PluginDataBatch {
  id: number
  batchId: string
  dataType: string
  count: number
  status: string
  processedCount: number
  failedCount: number
  processingTime: number
  createdAt: string
  updatedAt: string
  completedAt: string
  errorMessage: string
}

export interface PluginDataItem {
  id: number
  itemId: string
  batchId: string
  url: string
  method: string
  category: string
  status: string
  requestData: any
  responseData: any
  ruleInfo: any
  processedAt: string
  createdAt: string
  updatedAt: string
  errorMessage: string
}

export interface PageResult<T> {
  list: T[]
  pagination: {
    page: number
    pageSize: number
    total: number
  }
}

// 获取数据统计信息
export async function getDataStatistics() {
  return await http.get('/plugin/data/statistics')
}

// 获取批次列表
export async function getBatches(params: {
  page: number
  pageSize: number
  status?: string
}) {
  const queryParams = new URLSearchParams({
    page: params.page.toString(),
    pageSize: params.pageSize.toString(),
    ...(params.status ? { status: params.status } : {})
  })
  
  return await http.get(`/plugin/data/batches?${queryParams}`)
}

// 获取数据项列表
export async function getDataItems(params: {
  page: number
  pageSize: number
  batchId?: string
  category?: string
  status?: string
}) {
  const queryParams = new URLSearchParams({
    page: params.page.toString(),
    pageSize: params.pageSize.toString(),
    ...(params.batchId ? { batchId: params.batchId } : {}),
    ...(params.category ? { category: params.category } : {}),
    ...(params.status ? { status: params.status } : {})
  })
  
  return await http.get(`/plugin/data/items?${queryParams}`)
}

// 获取数据项详情
export async function getDataItemDetail(itemId: string) {
  return await http.get(`/plugin/data/items/${itemId}`)
}

// 重新处理数据项
export async function reprocessDataItem(itemId: string) {
  return await http.post(`/plugin/data/items/${itemId}/reprocess`)
}

// 删除数据项
export async function deleteDataItem(itemId: string) {
  return await http.delete(`/plugin/data/items/${itemId}`)
}